﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fuzhu;
using Dapper;
using LKU8.shoukuan.Model;
using ZDEntity;

namespace LKU8.shoukuan
{
    public class SCJHService
    {


        /// <summary>
        /// 检查U8生产计划是否已存在，根据日期+生产订单+线别+存货编码
        /// </summary>
        /// <param name="entity">生产计划实体</param>
        /// <param name="constr">连接字符串</param>
        /// <returns>是否存在</returns> 
        public static bool Exists(WanJi_SCJH_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"select count(*) as sl
from zdy_wanji_jihua 
where convert(varchar(20),ddate,23) =convert(varchar(20), @dDate,23) and cMocode = @cMocode and cXianbie = @cXianbie and cInvcode = @cInvcode
and cBanCi =@cBanci";

                int count = conn.Query<int>(sql, entity).First();
                return count > 0;
            }
        }

        /// <summary>
        /// 读取线别信息
        /// </summary>
        /// <param name="entity">生产计划实体</param>
        /// <param name="constr">连接字符串</param>
        /// <returns>是否存在</returns> 
        public static WanJi_XianBie_Entity GetXianBieEntity(string _XianBie, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"select cxianbie,cgongxu, cchangqu from zdy_wanji_xianbie
 
where cxianbie = @cxianbie  ";

                WanJi_XianBie_Entity entity = conn.Query<WanJi_XianBie_Entity>(sql, new { cxianbie = _XianBie }).FirstOrDefault();
                return entity;
            }
        }
        /// <summary>
        /// 读取单拼点数等信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="constr"></param>
        /// <returns></returns>
        public static zdy_wanji_pinban GetPinBan(WanJi_SCJH_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                if (entity.cInvcode.StartsWith("WF") || entity.cInvcode.StartsWith("ZF"))
                {
                    string sql = @"SELECT isczq,idpds,ipbsl  FROM zdy_wanji_pinban 
where   cinvcode= @cinvcode ";
                    zdy_wanji_pinban pinban = conn.Query<zdy_wanji_pinban>(sql, entity).FirstOrDefault();
                    return pinban;
                }
                else
                {
                    string sql = string.Format(@"SELECT isczq,idpds,ipbsl  FROM zdy_wanji_pinban a,zdy_wanji_xianbie b 
where a.cxbclass = b.cxbclass and b.cxianbie =@cxianbie and  cinvcode=  @cinvcode ");
                    zdy_wanji_pinban pinban = conn.Query<zdy_wanji_pinban>(sql, entity).FirstOrDefault();
                    return pinban;
                }

            }
        }

         /// <summary>
        /// 读取读取客户档案信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="constr"></param>
        /// <returns></returns>
        public static string  GetCusCode(WanJi_SCJH_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                
                    string sql = @"SELECT ccuscode  FROM zdy_wanji_cusinv    WHERE cinvcode= @cinvcode ";
                    string cCusCode = conn.Query<string>(sql, entity).FirstOrDefault();
                    return cCusCode;
                

            }
        }
        

    }

        
 
}
